<template>
    <view class="pages" :class="GlobalThemes.commonBackGroundClass">
        <view class="search-top-box" id="navbar">
            <u-navbar
                back-icon-color="#ffffff"
                :is-back="true"
                :custom-back="GoBack"
                :border-bottom="false"
                :background="typelist[tabIndex].background"
            >
                <view class="mp-name">
                    <text>{{ i18n('index').appName }}</text>
                </view>
            </u-navbar>
        </view>
        <view class="form-box">
            <!-- #ifdef APP-PLUS -->
            <view class="form-box-item"><text>请填写兑换码</text></view>
            <view class="form-box-item">
                <u-input
                    type="text"
                    v-model="formData.code"
                    @input="onInput"
                    placeholder="字母或数组合"
                    :border="true"
                />
            </view>
            <view class="form-box-item">
                <u-button
                    :loading="saveLoading"
                    type="info"
                    :disabled="saveLoading"
                    @click="save"
                >
                    提交
                </u-button>
            </view>
            <!-- #endif -->
            <!-- #ifdef MP -->
            <view class="form-box-item">
                <text>Do you want to build a snowman ~~o(￣ε￣*)</text>
            </view>
            <view class="form-box-item">
                <u-input
                    type="text"
                    v-model="formData.code"
                    @input="onInput"
                    :placeholder="i18n('user').giftPlaceholder"
                    :border="true"
                />
            </view>
            <view class="form-box-item">
                <u-button
                    :loading="saveLoading"
                    type="info"
                    :disabled="saveLoading"
                    @click="save"
                >
                    扔过来吧
                </u-button>
            </view>
            <!-- #endif -->
        </view>
        <!-- #ifdef MP-WEIXIN -->
        <!-- 广告 -->
        <Adview
            v-if="adSwitchKg"
            :adSwitchKg="adSwitchKg"
            :homeADList="oneAd"
        ></Adview>
        <!-- #endif -->
    </view>
</template>

<script>
import Adview from '@/components/home-ad/ad-view.vue';
const MyApp = getApp();
const globalData = MyApp.globalData;
export default {
    components: {
        Adview
    },
    data() {
        return {
            adSwitchKg:
                globalData.adSwitchKg ||
                uni.getStorageSync('sys_config').ad_switch,
            oneAd: MyApp.getOneAd('alert_public'),
            tabIndex: 0,
            typelist: globalData.typelist,
            tarbarObj: globalData.tarbarObj,
            formData: {
                code: ''
            },
            saveLoading: false
        };
    },
    onReady() {},
    onShow() {
        MyApp.checkLogin(true);
    },
    onLoad(options) {
        if (options && options.hasOwnProperty('code')) {
            this.formData.code = options.code;
            uni.setClipboardData({
                data: '',
                success: function() {
                    uni.hideToast();
                }
            });
        }
    },
    computed: {
        i18n() {
            return globalData.$t;
        }
    },
    methods: {
        onInput(value) {
            if (!value || value == ' ') {
                return '';
            }
            this.$nextTick(function() {
                this.formData.code = value.replace(/[\W]/g, '');
            });
        },
        save() {
            let _this = this;
            _this.saveLoading = true;
            _this.$http
                .post('/codekey/codekey/useCodekey', _this.formData)
                .then(res => {
                    _this.saveLoading = false;
                    if (res.code == 200) {
                        uni.showModal({
                            title: _this.i18n('common').modelTitle,
                            content: res.msg,
                            showCancel: false,
                            success() {
                                MyApp.goHome();
                            }
                        });
                    }
                })
                .catch(err => {
                    _this.saveLoading = false;
                });
        }
    }
};
</script>

<style lang="scss">
.pages {
    .form-box {
        padding: 0 30upx;
        .form-box-item {
            padding: 20upx 0;
        }
    }
}
</style>
